﻿using System;
using System.Collections.Generic;
using ServiceLocator;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading;
using FileSystemCustomClasses.Framework;

namespace FileSystemCustomClasses.FileBrowserControl
{

	public class FileBrowserViewModel : INotifyPropertyChanged
	{
		private IoFavorite _currentFavorite;
		public ObservableCollection<FileSystemItem> FileList { get; set; }
		public ObservableCollection<IoFavorite> DropdownFavorites { get; set; }

		public FileSystemItem CurrentItem { get; set; }
		public IoFavorite CurrentFavorite
		{
			get { return _currentFavorite; }
			set
			{
				_currentFavorite = value;
				OnPropertyChanged();
			}
		}
		public String CurrentPath { get; set; }

		public RelayCommand FavoriteClickCommand { get; set; }
		public RelayCommand NavigateUpCommand { get; set; }

		private IFileSystemEngine _fileSystemEngine { get; set; }
		private bool _splitButtonInitialized;
		private DirectoryInfo _currentDir;
		private bool _canNavigateUp;

		public FileBrowserViewModel()
		{
			DropdownFavorites = new ObservableCollection<IoFavorite>();
			FileList = new ObservableCollection<FileSystemItem>();

			SynchronizationContext currentContext = Locator.Resolve<SynchronizationContext>();

			_fileSystemEngine = new FileSystemEngine(currentContext);
			_fileSystemEngine.OnUpdate += ListOnUpdate;

			FavoriteClickCommand = new RelayCommand(FavoriteClick);
			NavigateUpCommand = new RelayCommand(NavigateUp, CanNavigateUp);

		}

		public void SetPath(String argPath)
		{
			//TODO: Get DirectoryInfo and determine the navigation of this directory
			_currentDir = new DirectoryInfo(argPath);
			_canNavigateUp = (_currentDir.Parent != null);

			FileList.Clear();
			_fileSystemEngine.GetFileList(argPath);

			NavigateUpCommand.RaiseCanExecuteChanged();
		}

		public void AddFavorite(IoFavorite argFavorite)
		{
			DropdownFavorites.Add(argFavorite);

			if (DropdownFavorites.Count == 1)
			{
				CurrentFavorite = DropdownFavorites[0];
			}
		}

		public void AddFavoriteRange(List<IoFavorite> argFavorites)
		{
			foreach (IoFavorite currentFavorite in argFavorites)
			{
				AddFavorite(currentFavorite);
			}
		}

		public void ActivateCurrentItem()
		{
			FileSystemItem localItem = CurrentItem;

			if (localItem is DirectoryItem)
			{
				SetPath(localItem.Path);
			}

			if (CurrentItem is FileItem)
			{ }

		}

		private void FavoriteClick()
		{
			if (!_splitButtonInitialized)
			{
				_splitButtonInitialized = true;
				return;
			}

			int iLoop = 10;

		}

		private void ListOnUpdate(object argSender, FileListUpdateArgs argE)
		{
			foreach (FileSystemItem currentItem in argE.ItemList)
			{
				FileList.Add(currentItem);
			}
		}

		private bool CanNavigateUp()
		{
			return _canNavigateUp;
		}

		private void NavigateUp()
		{
			//Navigate to the parent dir
			SetPath(_currentDir.Parent.FullName);
		}

		public event PropertyChangedEventHandler PropertyChanged;

		protected virtual void OnPropertyChanged([CallerMemberName] string argPropertyName = null)
		{
			var handler = PropertyChanged;
			if (handler != null) handler(this, new PropertyChangedEventArgs(argPropertyName));
		}


	}
}
